package com.front.web.lvyouFood;

import com.bootdo.common.utils.GPSUtil;
import com.bootdo.common.utils.R;
import com.bootdo.common.utils.StringUtils;
import com.bootdo.haojiangboutils.MapBuild;
import com.bootdo.lvyou.domain.*;
import com.bootdo.lvyou.service.LvyouPunchCardService;
import com.bootdo.lvyou.service.LvyouScenicSpotService;
import com.bootdo.lvyou.service.LvyouScenicTicketOrderService;
import com.bootdo.maputils.GpsJisuanUitils;
import com.front.web.lvyouXcxWeb.WebPageController;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.geom.Point2D;
import java.util.*;

import static java.util.Comparator.comparing;
import static java.util.stream.Collectors.collectingAndThen;
import static java.util.stream.Collectors.toCollection;

@RestController
@RequestMapping("/f/lvyou/punchCardController")
public class PunchCardController extends BaseFoodShopController{

    /**
     * 根据景点id 留言打卡(目前弃用)
     * @param scenicId
     * @return
     */
    @RequestMapping("punchCardMessageSite")
    public R punchCardMessageSite(@RequestParam(value = "scenicId",defaultValue = "")Long scenicId,//景区id
                              @RequestParam(value = "content",defaultValue = "")String content,
                              @RequestParam(value = "punchCardId",defaultValue = "")Long punchCardId,//被回复的打卡id
                              @RequestParam(value = "img",defaultValue = "")String imgUrl,//非必填项
                              @RequestParam(value = "memberId",defaultValue = "")Long memberId,
                              @RequestParam(value = "ResponseId",defaultValue = "")Long ResponseId,
                              @RequestParam(value = "lng",defaultValue = "")Double lng,//当前坐标
                              @RequestParam(value = "lat",defaultValue = "")Double lat,//当前坐标
                              HttpServletRequest request, HttpServletResponse response){  //无值为留言 有值为回复    回复者Id
        boolean b = checkIsDiv(scenicId, response, request, lng, lat);
        if(punchCardId==null){
            if(!b){
                return R.error("不在景区内无法打卡");
            }
        }

        return setWritingCard(scenicId,content,punchCardId,imgUrl,memberId, ResponseId,  lng,lat, request, response);
    }
    /**
     * 根据景点id 留言打卡(带位置)
     * @param scenicId
     * @return
     */
    @RequestMapping("punchCardMessage")
    public R punchCardMessage(@RequestParam(value = "scenicId",defaultValue = "")Long scenicId,//景区id
                              @RequestParam(value = "content",defaultValue = "")String content,
                              @RequestParam(value = "punchCardId",defaultValue = "")Long punchCardId,//被回复的打卡id
                              @RequestParam(value = "img",defaultValue = "")String imgUrl,//非必填项
                              @RequestParam(value = "memberId",defaultValue = "")Long memberId,
                              @RequestParam(value = "ResponseId",defaultValue = "")Long ResponseId,
                              @RequestParam(value = "lng",defaultValue = "")Double lng,//当前坐标
                              @RequestParam(value = "lat",defaultValue = "")Double lat,//当前坐标
                              HttpServletRequest request, HttpServletResponse response){  //无值为留言 有值为回复    回复者Id
        if(lng!=null){
            boolean b = checkIsDiv(scenicId, response, request, lng, lat);
            if(punchCardId==null){
                if(!b){
                    return R.error("不在景区内无法打卡");
                }
            }
        }

        return setWritingCard(scenicId,content,punchCardId,imgUrl,memberId, ResponseId,  lng,lat, request, response);
    }


    public R setWritingCard(Long scenicId,//景区id
                        String content,Long punchCardId,//被回复的打卡id
                        String imgUrl,//非必填项
                        Long memberId,Long ResponseId, Double lng,//当前坐标
                        Double lat,//当前坐标
                        HttpServletRequest request, HttpServletResponse response){
        LvyouPunchCard lvyouPunchCard =new LvyouPunchCard();
        LvyouMember lvyouMember=null;
        try {
            lvyouMember = lvyouMemberService.get(memberId);
        }catch (Exception e){
            if(punchCardId==null){
                return R.ok("参数有误");
            }
            LvyouPunchCard card = lvyouPunchCardService.get(punchCardId);
            try {
                lvyouMember = lvyouMemberService.get(card.getMemberId());
            }catch (Exception e1){
                return R.error("该用户已被叫停，无法互动");
            }

        }


        LvyouScenicSpot lvyouScenicSpot = lvyouScenicSpotService.get(scenicId);
        if(lvyouScenicSpot==null){
            return R.error("该景区不存在");
        }
        if(ResponseId==null){//null值为 parentId 为0 true 留言   非null值为回复
            lvyouPunchCard.setScenicId(scenicId);
            lvyouPunchCard.setParentId(0L);
            lvyouPunchCard.setScenicName(lvyouScenicSpot.getName());
            lvyouPunchCard.setResponseId(0L);
            lvyouPunchCard.setMemberId(memberId);
            lvyouPunchCard.setMemberName(lvyouMember.getNickname());
            lvyouPunchCard.setMemberAvatar(lvyouMember.getAvatar());
            lvyouPunchCard.setImgUrl(imgUrl);
            lvyouPunchCard.setContent(content);
            lvyouPunchCard.setCardType(1+"");
            lvyouPunchCard.setIsRead(1);
        }else{
            LvyouMember ResponseMember = lvyouMemberService.get(ResponseId);//查询回复者的id
            lvyouPunchCard.setScenicId(scenicId);
            lvyouPunchCard.setParentId(punchCardId);//父id
            lvyouPunchCard.setScenicName(lvyouScenicSpot.getName());//景区名称
            lvyouPunchCard.setResponseId(ResponseId);//回复者id
            lvyouPunchCard.setResponseName(ResponseMember.getNickname());//回复者名称
            lvyouPunchCard.setResponseAvatar(ResponseMember.getAvatar());//回复者头像
            lvyouPunchCard.setMemberId(memberId);//被回复id
            lvyouPunchCard.setMemberName(lvyouMember.getNickname());//被回复名称
            lvyouPunchCard.setMemberAvatar(lvyouMember.getAvatar());//被回复头像
            lvyouPunchCard.setCardType(1+"");
            LvyouPunchCard topObject = lvyouPunchCardService.get(punchCardId);//查询一下  父id 是否为顶级
            if(topObject.getParentId()==0||topObject.getParentId()==null){//如果为null  或者为0  则为顶级id  将id 放入 topId
                lvyouPunchCard.setTopId(punchCardId);
            }else{
                lvyouPunchCard.setTopId(topObject.getTopId());
            }
            /*lvyouPunchCard.setImgUrl(imgUrl);*///回复
            lvyouPunchCard.setContent(content);//回复内容
            lvyouPunchCard.setIsRead(0);
        }


        lvyouPunchCardService.save(lvyouPunchCard);
        return R.ok().put("i",lvyouPunchCard);

    }


    /**
     * 根据景点id 写旅游评论
     * @param scenicId
     * @return
     */
    @RequestMapping("punchCardMessageLvyou")
    public R punchCardMessageJingQu(@RequestParam(value = "scenicId",defaultValue = "")Long scenicId,//景区id
                              @RequestParam(value = "content",defaultValue = "")String content,
                              @RequestParam(value = "punchCardId",defaultValue = "")Long punchCardId,//被回复的打卡id
                              @RequestParam(value = "img",defaultValue = "")String imgUrl,//非必填项
                              @RequestParam(value = "memberId",defaultValue = "")Long memberId,
                              @RequestParam(value = "ResponseId",defaultValue = "")Long ResponseId,
                              HttpServletRequest request, HttpServletResponse response){  //无值为留言 有值为回复    回复者Id

        R r = setWriting(scenicId, content, punchCardId, imgUrl, memberId, ResponseId, request, response);
        return r;

    }

    public R setWriting(Long scenicId,//景区id
                        String content,Long punchCardId,//被回复的打卡id
                        String imgUrl,//非必填项
                       Long memberId,Long ResponseId,HttpServletRequest request, HttpServletResponse response){

        LvyouPunchCard lvyouPunchCard =new LvyouPunchCard();
        LvyouMember lvyouMember=null;
        try {
            lvyouMember = lvyouMemberService.get(memberId);
        }catch (Exception e){
            if(punchCardId==null){
                return R.ok("参数有误");
            }
            LvyouPunchCard card = lvyouPunchCardService.get(punchCardId);
            lvyouMember = lvyouMemberService.get(card.getMemberId());
        }


        LvyouScenicSpot lvyouScenicSpot = lvyouScenicSpotService.get(scenicId);
        if(lvyouScenicSpot==null){
            return R.error("该景区不存在");
        }
        if(ResponseId==null){//null值为 parentId 为0 true 留言   非null值为回复
            lvyouPunchCard.setScenicId(scenicId);
            lvyouPunchCard.setParentId(0L);
            lvyouPunchCard.setScenicName(lvyouScenicSpot.getName());
            lvyouPunchCard.setResponseId(0L);
            lvyouPunchCard.setMemberId(memberId);
            lvyouPunchCard.setMemberName(lvyouMember.getNickname());
            lvyouPunchCard.setMemberAvatar(lvyouMember.getAvatar());
            lvyouPunchCard.setImgUrl(imgUrl);
            lvyouPunchCard.setContent(content);
            lvyouPunchCard.setCardType(2+"");
            lvyouPunchCard.setIsRead(1);
        }else{
            LvyouMember ResponseMember = lvyouMemberService.get(ResponseId);//查询回复者的id
            lvyouPunchCard.setScenicId(scenicId);
            lvyouPunchCard.setParentId(punchCardId);//父id
            lvyouPunchCard.setScenicName(lvyouScenicSpot.getName());//景区名称
            lvyouPunchCard.setResponseId(ResponseId);//回复者id
            lvyouPunchCard.setResponseName(ResponseMember.getNickname());//回复者名称
            lvyouPunchCard.setResponseAvatar(ResponseMember.getAvatar());//回复者头像
            lvyouPunchCard.setMemberId(memberId);//被回复id
            lvyouPunchCard.setMemberName(lvyouMember.getNickname());//被回复名称
            lvyouPunchCard.setMemberAvatar(lvyouMember.getAvatar());//被回复头像
            lvyouPunchCard.setCardType(2+"");
            LvyouPunchCard topObject = lvyouPunchCardService.get(punchCardId);//查询一下  父id 是否为顶级
            if(topObject.getParentId()==0||topObject.getParentId()==null){//如果为null  或者为0  则为顶级id  将id 放入 topId
                lvyouPunchCard.setTopId(punchCardId);
            }else{
                lvyouPunchCard.setTopId(topObject.getTopId());
            }
            /*lvyouPunchCard.setImgUrl(imgUrl);*///回复
            lvyouPunchCard.setContent(content);//回复内容
            lvyouPunchCard.setIsRead(0);
        }


        lvyouPunchCardService.save(lvyouPunchCard);
        return R.ok();
    }
    /**
     * 判断是否在景区内
     * @param spotId
     * @param response
     * @param request
     * @param lon
     * @param lat
     * @return
     */
    public boolean checkIsDiv(Long spotId,HttpServletResponse response,HttpServletRequest request,Double lon,Double lat){
        List<LvyouScenicMarker> list =  lvyouScenicMarkerService
                .list(MapBuild.getInstand().setKV("type",999)
                        .setKV("refId", spotId)
                );
        if(StringUtils.isEmpty(request.getParameter("b"))){
            double gps [] = GPSUtil.gcj02_To_Bd09(lat, lon);
            lat = gps[0];
            lon = gps[1];
        }
        Point2D.Double my = new Point2D.Double(lon,lat);
        List<Point2D.Double> pts = new ArrayList<>();
        list.forEach(item ->{
            Point2D.Double p2d = new Point2D.Double();
            p2d.setLocation(item.getLon(),item.getLat());
            pts.add(p2d);
        });
        boolean b =  GpsJisuanUitils.isInPolygon(my,pts);
        return b;
    }
    /**
     * 获取留言列表
     * @param scenicId
     * @param pageNo
     * @param pageSize
     * @return
     */
    @RequestMapping("getMessageList")
    public R getMessageList(@RequestParam(value = "scenicId",defaultValue = "")Long scenicId,
                            @RequestParam(value = "pageNo",defaultValue = "")Integer pageNo,
                            @RequestParam(value = "pageSize",defaultValue = "")Integer pageSize,
                            @RequestParam(value = "type",defaultValue = "")Integer type){//1  为互动打卡   2为旅游点评

        HashMap<String, Object> param = new HashMap<>();
        param.put("scenicId",scenicId);


        param.put("parentId","0");
        param.put("cardType",type);
        param.put("sort","id");
        param.put("order","desc");
        List<LvyouPunchCard> listSize = lvyouPunchCardService.list(param);
        if(pageNo!=null){
            param.put("offset",(pageNo-1)*pageSize);
            param.put("limit",pageSize);
        }
        List<LvyouPunchCard> list = lvyouPunchCardService.list(param);

        for (LvyouPunchCard card:list){
            LvyouMember lvyouMember = lvyouMemberService.get(card.getMemberId());
            if(lvyouMember==null){
                continue;
            }
            card.setMemberAvatar(lvyouMember.getAvatar());
            String imgUrl = card.getImgUrl();
            if (StringUtils.isNotEmpty(imgUrl)) {
                if (card.getImgUrl().contains(",")) {
                    String[] split = card.getImgUrl().split(",");

                    card.setMediaUrlList(split);
                } else {
                    String[] strings = new String[1];
                    strings[0] = card.getImgUrl();
                    card.setMediaUrlList(strings);

                }
            }

            List<LvyouPunchCard>  byList=lvyouPunchCardService.getByParentId(card.getId());
            HashMap<String, Object> param2 = new HashMap<>();
            param2.put("scenicId",scenicId);
            param2.put("topId",card.getId());
            int count = lvyouPunchCardService.count(param2);
            if(byList.size()>0&&byList!=null){

                LvyouPunchCard card1 = byList.get(0);

                HashMap<String, Object> params = new HashMap<>();
                params.put("memberId",card1.getResponseId());
                params.put("memberName",card1.getResponseName());
                params.put("content",card1.getContent());
                params.put("count",count);
                card.setMap(params);
            }

        }

        List<LvyouPunchCard> listNew = listSize.stream().collect(
                collectingAndThen(
                        toCollection(() -> new TreeSet<>(comparing(LvyouPunchCard::getMemberId))), ArrayList::new));
        return R.ok().put("l",list).put("count",listNew.size());
    }

    /**
     * 根据留言Id 查询所有回复
     * @param punchCardId
     * @return
     */
    @RequestMapping("selectOtherMessage")
    public R selectOtherMessage(@RequestParam(value = "punchCardId",defaultValue = "")Long punchCardId,
                                @RequestParam(value = "pageNo", defaultValue = "")Integer pageNo,
                                @RequestParam(value = "pageSize", defaultValue = "")Integer pageSize){



        if (pageNo < 1) {
            return R.error("参数错误");
        }
        int offset = (pageNo -1) * pageSize;
        LvyouPunchCard lvyouPunchCard = lvyouPunchCardService.get(punchCardId);
       /* ArrayList<LvyouPunchCard> lvyouPunchCards = new ArrayList<>();
        List<LvyouPunchCard> list = recursion(lvyouPunchCard, lvyouPunchCards);*/


        List<LvyouPunchCard> list = lvyouPunchCardService.getByParentIdLimit(punchCardId,offset,pageSize);
        if(list.size()==0&&list==null){
            return R.error("暂无数据");
        }
        for(LvyouPunchCard card:list){
            Map map=lvyouMemberService.getByIdGetNamePhoto(card.getMemberId());
            card.setMemberAvatar((String)map.get("avatar"));
            card.setMemberName((String)map.get("nickname"));

            Map map2=lvyouMemberService.getByIdGetNamePhoto(card.getResponseId());
            card.setResponseAvatar((String)map2.get("avatar"));
            card.setResponseName((String)map2.get("nickname"));
          }
        return R.ok().put("l",list);
    }

    public List<LvyouPunchCard> recursion(LvyouPunchCard lvyouPunchCard,List<LvyouPunchCard> list){
        List<LvyouPunchCard> byParentId = lvyouPunchCardService.getByParentId(lvyouPunchCard.getId());
        if(byParentId!=null){
            for (LvyouPunchCard lvyouPunchCard1:byParentId){
                list.add(lvyouPunchCard1);
                recursion(lvyouPunchCard1, list);

            }
            return list;
        }
        return list;
    }


    /**
     * 获取头像列表信息
     * @return
     */
    @RequestMapping("getMessageAvatar")
    public R getMessageAvatar(@RequestParam(value = "pageNo",defaultValue ="" )Integer pageNo,
                              @RequestParam(value = "pageSize",defaultValue ="" )Integer pageSize){

        HashMap<String, Object> params = new HashMap<>();
        params.put("limit",pageSize);
        params.put("offset",(pageNo-1)*pageSize);
        List<LvyouScenicSpot> list1 = lvyouScenicSpotService.simpleList(params);
        for(LvyouScenicSpot lvyouScenicSpot:list1){
            HashMap<String, Object> param = new HashMap<>();
            param.put("scenicId",lvyouScenicSpot.getId());
            param.put("offset",0);
            param.put("limit",20);
            param.put("parentId","0");
            List<LvyouPunchCard> list = lvyouPunchCardService.list(param);
            List<LvyouAvatarList> avaratList =new ArrayList();
            for (LvyouPunchCard lvyouPunchCard:list){
                LvyouAvatarList avatarList =new LvyouAvatarList();
                avatarList.setMemberId(lvyouPunchCard.getMemberId());
                avatarList.setMemberAvatar(lvyouPunchCard.getMemberAvatar());
                avaratList.add(avatarList);
            }

            List<LvyouAvatarList> listNew = avaratList.stream().collect(
                    collectingAndThen(
                            toCollection(() -> new TreeSet<>(comparing(LvyouAvatarList::getMemberId))), ArrayList::new));
            String[] strings = new String[listNew.size()];
            Integer number=0;
            for(LvyouAvatarList lvyouAvatarList:listNew){
                LvyouMember lvyouMember = lvyouMemberService.get(lvyouAvatarList.getMemberId());
                if(lvyouMember!=null){
                    strings[number]=lvyouMember.getAvatar();
                    number++;
                }else{
                    strings[number]=lvyouAvatarList.getMemberAvatar();
                    number++;
                }
            }
            lvyouScenicSpot.setListAvatarNew(listNew);
            lvyouScenicSpot.setAvatarArray(strings);
            lvyouScenicSpot.setCardNumber(number);
        }


        return R.ok().put("l",list1);
    }


    /**
     * 获得打卡记录
     * @param memberId
     * @return
     */
    @RequestMapping("getMemberPunchCardRecord")
    public R getMemberPunchCardRecord(@RequestParam(value = "memberId",defaultValue = "")Long memberId){
        List<LvyouPunchCard> list = lvyouPunchCardService.getByParentIidAndMemberId(memberId, 0L);//打卡记录
        if(list.size()==0||list==null){
            return R.error("暂无数据");
        }
        List<LvyouPunchCard> listNew = list.stream().collect(//新打卡记录
                collectingAndThen(
                        toCollection(() -> new TreeSet<>(comparing(LvyouPunchCard::getScenicId))), ArrayList::new));
        ArrayList<LvyouScenicSpot> ScenicSpotsList = new ArrayList<>();
        for(LvyouPunchCard lvyouPunchCard:listNew){
            Long scenicId = lvyouPunchCard.getScenicId();
            LvyouScenicSpot lvyouScenicSpot = lvyouScenicSpotService.getNoDesc(scenicId);
            List<LvyouPunchCard> byParentIidAndMemberIdAndScenicSopt = lvyouPunchCardService.getByParentIidAndMemberIdAndScenicSopt(memberId, 0L, scenicId);

            for (LvyouPunchCard card:byParentIidAndMemberIdAndScenicSopt) {
                LvyouMember lvyouMember = lvyouMemberService.get(card.getMemberId());
                if (lvyouMember == null) {
                    continue;
                }
                card.setMemberAvatar(lvyouMember.getAvatar());
                String imgUrl = card.getImgUrl();
                if (StringUtils.isNotEmpty(imgUrl)) {
                    if (card.getImgUrl().contains(",")) {
                        String[] split = card.getImgUrl().split(",");

                        card.setMediaUrlList(split);
                    } else {
                        String[] strings = new String[1];
                        strings[0] = card.getImgUrl();
                        card.setMediaUrlList(strings);

                    }
                }
            }

            lvyouScenicSpot.setCardList(byParentIidAndMemberIdAndScenicSopt);
            ScenicSpotsList.add(lvyouScenicSpot);
        }

        return R.ok().put("l",ScenicSpotsList);

    }

    /**
     * 查看未读的新消息   互动消息
     * @param memberId
     * @return
     */
    @RequestMapping("selectIsReadCardMessage")
    public R selectIsReadCardMessage(@RequestParam(value = "memberId",defaultValue = "")Long memberId,
                                     @RequestParam(value = "pageSize",defaultValue = "")Integer pageSize,
                                     @RequestParam(value = "pageNo",defaultValue = "")Integer pageNo){
        HashMap<String, Object> param = new HashMap<>();
        param.put("memberId",memberId);
        param.put("offset",(pageNo-1)*pageSize);
        param.put("limit",pageSize);
        param.put("sort","create_time");
        param.put("order","desc");
        param.put("parent_id","desc");
        List<LvyouPunchCard> list =  lvyouPunchCardService.list(param);
/*       List<LvyouPunchCard> list =  lvyouPunchCardService.selectIsReadCardMessage(memberId);*/
       for (LvyouPunchCard lvyouPunchCard:list){

           Long punchCardId = getPunchCardId(lvyouPunchCard.getParentId());
           try {
               LvyouPunchCard lvyouPunchCard1 = lvyouPunchCardService.get(punchCardId);
               lvyouPunchCard.setLvyouPunchCard(lvyouPunchCard1);
           }catch (Exception e){

               e.printStackTrace();
           }
           LvyouPunchCard lvyouPunchCard1 = lvyouPunchCardService.get(lvyouPunchCard.getId());
           if(lvyouPunchCard1!=null){
               Long memberId1 = lvyouPunchCard1.getMemberId();
               if(memberId1!=null){
                   String avatar = commonsGetAvatar(memberId1);
                   lvyouPunchCard.setMemberAvatar(avatar);
               }
               Long responseId = lvyouPunchCard1.getResponseId();
               if(responseId!=null) {
                   String avatar = commonsGetAvatar(responseId);
                   lvyouPunchCard.setResponseAvatar(avatar);
               }
           }



           lvyouPunchCard1.setIsRead(1);//状态改为已读状态
           lvyouPunchCardService.save(lvyouPunchCard1);
       }
       return R.ok().put("l",list);

    }
    /**
     * 查看全部的消息   互动消息
     * @param memberId
     * @return
     */
    @RequestMapping("selectIsReadCardMessageAll")
    public R selectIsReadCardMessageAll(@RequestParam(value = "memberId",defaultValue = "")Long memberId,
                                        @RequestParam(value = "pageSize",defaultValue = "")Integer pageSize,
                                        @RequestParam(value = "pageNo",defaultValue = "")Integer pageNo){

        HashMap<String, Object> param = new HashMap<>();
        param.put("memberId",memberId);
        param.put("offset",(pageNo-1)*pageSize);
        param.put("limit",pageSize);
        param.put("sort","createTime");
        param.put("order","desc");
        List<LvyouPunchCard> list =  lvyouPunchCardService.list(param);

       return R.ok().put("l",list);

    }

    public String commonsGetAvatar(Long id){
        LvyouMember lvyouMember = lvyouMemberService.get(id);
        String avatar=null;
        if(lvyouMember!=null){
            avatar = lvyouMember.getAvatar();
        }

        return avatar;
    }



    /**
     * 递归拿取最顶级
     * @param pinglunId
     * @return
     */
    public Long getPunchCardId(Long pinglunId){
        LvyouPunchCard card = lvyouPunchCardService.get(pinglunId);
        Long punchCardId =pinglunId;
        if(card!=null){
            if(card.getParentId()!=0){
                punchCardId = getPunchCardId(card.getParentId());
            }
        }
        return punchCardId;
    }
    /**
     * 查看是否有未读的新消息   互动消息 多少条
     * @param memberId
     * @return
     */
    @RequestMapping("selectIsReadCardMessageNumber")
    public R selectIsReadCardMessageNumber(@RequestParam(value = "memberId",defaultValue = "")Long memberId){
        Integer count =0;
        try {

            count =lvyouPunchCardService.selectIsReadCardMessageNumber(memberId);
        }catch (Exception e){
            e.printStackTrace();
        }

        if(count==0||count==null){
            return R.ok();
        }
        return R.ok().put("count",count);

    }

    /**
     * 获取打卡景点数量
     * @param memberId
     * @return
     */
    @RequestMapping("getPunchCardScenicNumber")
    public R getPunchCardScenicNumber(@RequestParam(value = "memberId",defaultValue = "")Long memberId,@RequestParam(value = "type",defaultValue = "1")Integer type){

        LvyouMember lvyouMember = lvyouMemberService.get(memberId);

        List<LvyouPunchCard> list = lvyouPunchCardService.getByParentIidAndMemberId(memberId, 0L);//打卡记录
        HashMap<String, Object> param = new HashMap<>();

        param.put("memberMobile",lvyouMember.getMobile());
        param.put("status",5);
        List<LvyouScenicTicketOrder> order = lvyouScenicTicketOrderService.list(param);//去过的景点

        HashMap<String, Object> param2 = new HashMap<>();

        param2.put("memberMobile",lvyouMember.getMobile());
        param2.put("noZero",0);
        List<LvyouScenicTicketOrder> order2 = lvyouScenicTicketOrderService.list(param2);//全部订单


        List<LvyouScenicTicketOrder> newOrder = order.stream().collect(//新去过的景点
                collectingAndThen(
                        toCollection(() -> new TreeSet<>(comparing(LvyouScenicTicketOrder::getSpotId))), ArrayList::new));


        List<LvyouPunchCard> listNew = list.stream().collect(//新打卡记录
                collectingAndThen(
                        toCollection(() -> new TreeSet<>(comparing(LvyouPunchCard::getScenicId))), ArrayList::new));
        if(lvyouMember.getMobile()==null){
            return R.ok().put("count",listNew.size()).put("scenicCount",0).put("orderCount",0);
        }
        if(type==1){
            return R.ok().put("count",listNew.size()).put("scenicCount",newOrder.size()).put("orderCount",order2.size());
        }else{
            return R.ok().put("cardCount",listNew.size()).put("scenicCount",newOrder.size());
        }

    }

    /**
     *  获取去过的景点  浏览足迹
     * @param memberId
     * @param pageNo
     * @param pageSize
     * @return
     */
    @RequestMapping("getScenic")
    public R getScenic(@RequestParam(value = "memberId",defaultValue = "")Long memberId,
                       @RequestParam(value = "pageNo",defaultValue = "")Integer pageNo,
                       @RequestParam(value = "pageSize",defaultValue = "")Integer pageSize){
        LvyouMember lvyouMember = lvyouMemberService.get(memberId);
        if(lvyouMember==null){
            return R.error("请登录");
        }
        if(lvyouMember.getMobile()==null){
            return R.error("请绑定手机号码");
        }
        HashMap<String, Object> param = new HashMap<>();
        if(pageNo!=null){
            param.put("offset",(pageNo-1)*pageSize);
            param.put("limit",pageSize);
        }
        param.put("memberMobile",lvyouMember.getMobile());
        param.put("status",5);
        List<LvyouScenicTicketOrder> order = lvyouScenicTicketOrderService.list(param);//去过的景点

        List<LvyouScenicTicketOrder> newOrder = order.stream().collect(//新去过的景点
                collectingAndThen(
                        toCollection(() -> new TreeSet<>(comparing(LvyouScenicTicketOrder::getSpotId))), ArrayList::new));

        ArrayList<LvyouScenicSpot> lvyouScenicSpots = new ArrayList<>();
        for(LvyouScenicTicketOrder order1:newOrder){
            Long spotId = order1.getSpotId();
            LvyouScenicSpot lvyouScenicSpot = lvyouScenicSpotService.get(spotId);
            if(lvyouScenicSpot!=null){
                lvyouScenicSpots.add(lvyouScenicSpot);
            }

        }
        return R.ok().put("l",lvyouScenicSpots);
    }
}
